
import { _decorator, Component, Vec3, EventMouse, systemEvent, SystemEvent, AnimationComponent,view, EventTouch, SkeletalAnimation } from 'cc';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = PlayerController
 * DateTime = Sun Nov 28 2021 17:58:12 GMT+0800 (中国标准时间)
 * Author = 悟柒非梦
 * FileBasename = PlayerController.ts
 * FileBasenameNoExtension = PlayerController
 * URL = db://assets/Scripts/PlayerController.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/en/
 *
 */
 
@ccclass('PlayerController')
export class PlayerController extends Component {
    // [1]
    // dummy = '';
    private _startJump: boolean = false;
    private _jumpStep: number = 0;
    private _curJumpTime:number = 0;
    private _jumpTime:number = 0.3;// 每次跳跃需要的时长
    private _curJumpSpeed: number = 0;
    private _curPos: Vec3 = new Vec3();
    private _deltaPos: Vec3 = new Vec3(0,0,0);
    private _targetPos: Vec3 = new Vec3();

    private _curMoveIndex = 0;


    // [2]
    // @property
    // serializableDummy = 0;
    // @property({type: AnimationComponent})
    // public BodyAnim: AnimationComponent | null;

    @property({type: SkeletalAnimation})
    public CocosAnim: SkeletalAnimation|null = null;
    

    start () {
        //绑定鼠标点击事件 执行onMouseUp方法
        systemEvent.on(SystemEvent.EventType.MOUSE_UP, this.onMouseUp, this)
        // systemEvent.on(SystemEvent.EventType.TOUCH_END,this.onTouchStart,this)     
    }

    setInputActive(active:boolean){
        if(active){
            //绑定鼠标点击事件 执行onMouseUp方法
            systemEvent.on(SystemEvent.EventType.MOUSE_UP, this.onMouseUp, this)
        }else{
            systemEvent.off(SystemEvent.EventType.MOUSE_UP, this.onMouseUp, this)
        }
    }
    onTouchStart(event:any){
        const width = view.getFrameSize().width/2
        if(event.getLocation().x<=width){
            this.jumpByStep(1)
        }else{
            this.jumpByStep(2)
        }
         
    }

    onMouseUp(event: EventMouse){
        if(event.getButton() === 0){
            this.jumpByStep(1)
        }else if(event.getButton() === 2){
            this.jumpByStep(2)
        }
    }
    /**
     * 开始跳跃
     * @param step 跳跃的步数 
     * @returns 
     */
    jumpByStep( step:number){
        if(this._startJump) return
        // if(this.BodyAnim){
        //     if(step === 1){
        //         this.BodyAnim.play('oneStep')
        //     }else if(step === 2){
        //         this.BodyAnim.play('twoStep')
        //     }
        // }
    
        this._startJump = true;
        this._jumpStep = step;
        this._curJumpTime = 0;
        // 跳跃的速度 = 步数/时长 一次跳跃的步数越多，速度越快
        this._curJumpSpeed = this._jumpStep/this._jumpTime;
        this.node.getPosition(this._curPos);
        Vec3.add(this._targetPos,this._curPos,new Vec3(this._jumpStep,0,0))

        if (this.CocosAnim) {
            this.CocosAnim.getState('cocos_anim_jump').speed = 3; // 跳跃动画时间比较长，这里加速播放
            this.CocosAnim.play('cocos_anim_jump'); // 播放跳跃动画
        }
        this._curMoveIndex += step;
    }
    onOnceJumpEnd() {
        console.log('JumpEnd');
        if (this.CocosAnim) {
            this.CocosAnim.play('cocos_anim_idle');
        }
        this.node.emit('JumpEnd', this._curMoveIndex);
    }
    reset(){
        this._curMoveIndex = 0;
    }
    /**
     * 更新函数
     * @param deltaTime 增量时间
     */
    update (deltaTime: number) {
        if (this._startJump) {
            this._curJumpTime += deltaTime;
            if (this._curJumpTime > this._jumpTime) {// 程序执行时间大于跳跃的时间
                // 本次跳跃结束
                this.node.setPosition(this._targetPos);// 设置节点位置到目标位置
                this._startJump = false;// 跳跃结束
                this.onOnceJumpEnd();
            } else {
                // 本次跳跃进行中
                this.node.getPosition(this._curPos);// 获取节点当前的位置
                this._deltaPos.x = this._curJumpSpeed * deltaTime;// 增量移动横坐标 = 移动速度*增量时间
                Vec3.add(this._curPos, this._curPos, this._deltaPos);// 计算增量移动的节点的位置
                this.node.setPosition(this._curPos);//更新节点的位置
            }
        }
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/en/scripting/life-cycle-callbacks.html
 */
